package com.h5ve.toolkit.controller;

import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.h5ve.base.api.R;
import com.h5ve.base.constant.AppConstants;
import com.h5ve.base.util.StrUtil;
import com.h5ve.log.annotation.SysLog;
import com.h5ve.toolkit.entity.Form;
import com.h5ve.toolkit.entity.GroupInfo;
import com.h5ve.toolkit.service.FormService;
import com.h5ve.toolkit.service.GroupInfoService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分组信息
 *
 * @author h5ve
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstants.APP_TOOLKIT + "/groupInfo")
public class GroupInfoController {

    private final GroupInfoService groupInfoService;
    private final FormService formService;

    /**
     * 查询条件构建
     * @param groupInfo
     * @return R
     */
    private LambdaQueryWrapper<GroupInfo> getQueryWrapper(GroupInfo groupInfo) {
        return new LambdaQueryWrapper<GroupInfo>()
                .eq(!StrUtil.isEmptyIfNum(groupInfo.getApplicationId()), GroupInfo::getApplicationId, groupInfo.getApplicationId())
                .like(CharSequenceUtil.isNotBlank(groupInfo.getName()), GroupInfo::getName, groupInfo.getName())
                .eq(CharSequenceUtil.isNotBlank(groupInfo.getStatus()), GroupInfo::getStatus, groupInfo.getStatus());
    }

    /**
     * 分组信息列表
     * @param groupInfo
     * @return R
     */
    @GetMapping("/list")
    public R list(GroupInfo groupInfo) {
        List<GroupInfo> groupList = groupInfoService.list(getQueryWrapper(groupInfo));
        return R.ok(groupList, groupList.size());
    }

    /**
     * 分组信息
     * @param id 编号
     * @return R
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Integer id) {
        return R.ok(groupInfoService.getById(id));
    }

    /**
     * 分组信息新增
     * @param groupInfo
     * @return R
     */
    @SysLog("分组新增")
    @PostMapping("/save")
    public R save(@RequestBody GroupInfo groupInfo) {
        GroupInfo info = null;
        if (groupInfo.getParentId() != null) {
            info = groupInfoService.getById(groupInfo.getParentId());
            groupInfo.setParentIds(info.getParentIds() + "," + groupInfo.getParentId());
        } else {
            groupInfo.setParentId(0l);
            groupInfo.setParentIds("0");
        }
        groupInfoService.save(groupInfo);
        return R.ok();
    }

    /**
     * 分组信息修改
     * @param groupInfo
     * @return R
     */
    @SysLog("分组修改")
    @PutMapping("/update")
    public R update(@RequestBody GroupInfo groupInfo) {
        GroupInfo info = null;
        if (0 != groupInfo.getParentId()) {
            info = groupInfoService.getById(groupInfo.getParentId());
            groupInfo.setParentIds(info.getParentIds() + "," + groupInfo.getParentId());
        } else {
            groupInfo.setParentIds("0");
        }
        groupInfoService.updateById(groupInfo);
        //更新用户分组名称
        return R.ok();
    }

    /**
     * 分组信息删除
     * @param id
     * @return R
     */
    @SysLog("分组删除")
    @DeleteMapping("/remove/{id}")
    public R remove(@PathVariable("id") Integer id) {
        if (groupInfoService.list(new QueryWrapper<GroupInfo>().eq("parent_id", id)).size()>0) {
            return R.error("存在下级分组,不允许删除");
        }
        if (formService.list(new QueryWrapper<Form>().eq("group_id",id)).size()>0){
            return R.error("存在关联表单,不允许删除");
        }
        groupInfoService.removeById(id);
        return R.ok();
    }

    /**
     * 分组状态更改
     * @param groupInfo
     * @return R
     */
    @SysLog("分组状态更改")
    @GetMapping("/changeStatus")
    public R changeStatus(GroupInfo groupInfo) {
        groupInfoService.updateById(groupInfo);
        return R.ok();
    }

    /**
     * 加载分组列表树
     */
    @GetMapping("/groupTree")
    public R groupTree(GroupInfo groupInfo) {
        List<GroupInfo> groupList = groupInfoService.list(new LambdaQueryWrapper<GroupInfo>().eq(!StrUtil.isEmptyIfNum(groupInfo.getApplicationId()), GroupInfo::getApplicationId, groupInfo.getApplicationId()).orderByAsc(GroupInfo::getSort));
        GroupInfo groupInfo1 = new GroupInfo();
        groupInfo1.setId(0l);
        groupInfo1.setParentId(-1l);
        groupInfo1.setName("全部分组");
        //groupInfo1.setChildren(groupList);
        groupList.add(0, groupInfo1);
        return R.ok(groupInfoService.buildTree(groupList, -1));
    }
}
